package com.abc.xxx.controller;

import com.abc.xxx.model.Dict;
import com.abc.xxx.service.DictService;
import com.abc.xxx.util.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 本接口现阶段只提供针对租户的设备的操作
 */
@Slf4j
@RestController
@RequestMapping("/dicts")
public class DictController {

    @Autowired
    private DictService dictService;

    @GetMapping
    public Result findDicts() {
        String oper = "查询所有字典";
        log.info("====>{}", oper);
        List<Dict> list = dictService.list();
        QueryWrapper<Dict> w = new QueryWrapper<>();
        w.select("`type`,`code`,`codes`,`name`");
        return Result.success(oper).list(list);
    }

    @PostMapping
    public Result addDicts(@RequestBody String body) {
        String oper = "批量添加字典";
        log.info("====>{}, body: {}", oper,body);

        List<Dict> dicts = JSON.parseArray(body, Dict.class);
        dictService.saveBatch(dicts);
        return Result.success(oper);
    }


}
